package com.classeven.downhill.state;

import org.andengine.entity.scene.ITouchArea;
import org.andengine.input.touch.TouchEvent;

import android.view.KeyEvent;

import com.classeven.downhill.define.iGameDefines;
import com.classeven.downhill.element.ABackground;
import com.classeven.downhill.element.AMusic;
import com.classeven.downhill.element.AScene;
import com.classeven.downhill.element.ASprite;
import com.classeven.downhill.engine.GameEngine;
import com.classeven.downhill.engine.Utils;

public class State_MainMenu {

	ABackground bg_menu;
	ASprite btn_play, btn_customize;
	AScene scene;
	GameEngine engine;
	AMusic musik_menu;
	
	public State_MainMenu(GameEngine engine)
	{
		this.engine = engine;
		bg_menu = new ABackground(engine, iGameDefines.SPRITE_MENU, iGameDefines.MENU_BG);
		
		btn_play = new ASprite(engine, iGameDefines.SPRITE_MENU, iGameDefines.MENU_BTN_PLAY);
		
		btn_customize = new ASprite(engine, iGameDefines.SPRITE_MENU, iGameDefines.MENU_BTN_OPTION);
	
		musik_menu = new AMusic(engine, iGameDefines.BGM_GAME, 0);
		
		scene = new AScene(0);
		
	}
	
	public void init()
	{
		setPos();
		
		musik_menu.Play();
		engine.SetGameScene(scene);
			
		engine.SceneAttach(scene, bg_menu);
		engine.SceneAttach(scene, btn_play);
		engine.SceneAttach(scene, btn_customize);
//		engine.SceneAttach(scene, btn_about);
		
		
		scene.theScene().registerTouchArea(btn_play.theSprite());
		scene.theScene().setOnAreaTouchListener(engine);
		scene.theScene().setOnSceneTouchListener(engine);
	}
	
	private void setPos()
	{
		btn_customize.theSprite().setY(btn_customize.GetY() - Utils.GetRatioHeight(10));
		btn_play.theSprite().setY(btn_customize.GetY() - btn_play.theSprite().getHeight());
		
	}

	public void update()
	{
		
	}
	
	public void exit()
	{
		scene.theScene().detachChildren();
	}

	public void event(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea,
			float pTouchAreaLocalX, float pTouchAreaLocalY)
	{
		if(pTouchArea.equals(btn_play.theSprite()) && pSceneTouchEvent.isActionDown())
		{
			engine.SetNextMessageState(engine.NextMessageState());
		}
	}

	public void back(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK)
		{
			engine.finish();
		}
		
	}
	
}
